﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using Cosmetic.DataEntity;
using System.Configuration;
using System.Data.SqlClient;

namespace WebApp.Space.AccountInfo
{
    public partial class OrderRecords : PageBase
    {
        private UserManager m_UserManager = new UserManager();
        PurchaseManager m_PurchaseManager = new PurchaseManager();
        private OrderManager m_orderManger = new OrderManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.SignInUser == null)
            {
                Page.RegisterClientScriptBlock("key", "<script>window.top.location = '/Login.aspx';</script>");
                return;
            }
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            List<Order> theOrderList = m_PurchaseManager.getOrdersByUser_L(this.SignInUser); //这是该用户所有的订单记录。
            this.lvOrders.DataSource = theOrderList.OrderByDescending(c => c.CreateTime).ToList();
            this.lvOrders.DataBind();
        }

        protected void lvOrders_PagePropertiesChanged(object sender, EventArgs e)
        {
            Bind();
        }

        public string GetUserNameById(string UserId)
        {
            return m_UserManager.GetUserById(new Guid(UserId)).UserName;
        }

        protected void lvOrders_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "OnDel")
            {
                Literal litstate = e.Item.FindControl("litState") as Literal;
                //而已付款订单和退款订单用户是不能自己删除的。
                //状态：未付款、已付款、已发货、已退款、完成
                if (litstate.Text == "未付款")
                {
                    // cid = new Guid(e.CommandArgument.ToString());
                    string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
                    SqlConnection con = new SqlConnection(conStr);
                    con.Open();
                    string cmdStr = ("select * from OrderDetail where OrderId='" + e.CommandArgument.ToString() + "'");
                    SqlCommand com = new SqlCommand(cmdStr, con);
                    SqlDataAdapter da = new SqlDataAdapter(com);
                    SqlDataReader dr = com.ExecuteReader();
                    if (dr.HasRows == true)
                    {
                        OrderManager.DeleteOrderDetail_ByOrderID(new Guid(e.CommandArgument.ToString()));
                    }
                    OrderManager.DeleteSaleProducts(new Guid(e.CommandArgument.ToString()));
                }
            }
            else if (e.CommandName == "refund")
            {
                OrderManager.ModifyOrderState(e.CommandArgument.ToString(), 4);
            }
            else if (e.CommandName == "OnPay")
            {
                Order m_Order = m_orderManger.GetOrderById(new Guid(e.CommandArgument.ToString()));
                string url = "http://" + Request.Url.Authority + "/Shopping/ReturnS.aspx?TradeNo=" + m_Order.OrderNum;
                Response.Write("<script language=javascript> window.open('"+url+"','支付','height=600,width=1000,top=0,left=0')</script>");
            }
            Bind();
        }

        protected void lvOrders_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            LinkButton linbtn = e.Item.FindControl("LinkButton2") as LinkButton; //申请退款
            LinkButton linkbtnPay = e.Item.FindControl("LinkButton3") as LinkButton;//支付订单
            LinkButton linkDele = e.Item.FindControl("LinkButton1") as LinkButton;//删除
            Literal litState = e.Item.FindControl("litState") as Literal;//支付状态
            if (litState.Text == "未付款")
            {
                linbtn.Enabled = false;
                linkbtnPay.Enabled = true;
                linkDele.Enabled = true;
            }
            else if (litState.Text == "申请退款")
            {
                linbtn.Enabled = false;
                linkbtnPay.Enabled = false;
                linkDele.Enabled = false;
            }
            else
            {
                linkbtnPay.Enabled = false;
                linkDele.Enabled = false;
            }
        }
    }
}